package core

import (
	"yuanchang/spider/http"
	"time"
	"yuanchang/spider/util/conf"
)

type Downloader struct {
	WorkerCount  int
	requestChan  chan *http.Request
	responseChan chan *http.Response
}

func (d *Downloader) Commit(r *http.Request) {
	go func() {
		d.requestChan <- r
	}()
}

func (d *Downloader) GetResponse() chan *http.Response {
	return d.responseChan
}

func (d *Downloader) Start() {
	for i := 0; i < d.WorkerCount; i++ {
		go func() {

			var (
				delay time.Duration
			)

			if conf.UserConfig == nil {
				delay = conf.DefaultConfig.Downloader.Delay
			} else {
				delay = conf.UserConfig.Downloader.Delay
			}
			t := time.Tick(delay) // downloader delay
			for {
				if req := <-d.requestChan; req != nil {
					println("download: ", req.Url)
					if t != nil {
						<-t
					}
					res, err := req.Do()
					if err != nil {
						println("download error: ", err.Error())
						continue
					}
					d.responseChan <- res
				}
			}
		}()
	}
}

func (d *Downloader) Stop() {
	// TODO implements
}

func NewDownloader() *Downloader {
	var (
		workerCount int
	)
	if conf.UserConfig == nil {
		workerCount = conf.DefaultConfig.Downloader.Concurrent
	} else {
		workerCount = conf.UserConfig.Downloader.Concurrent
	}

	return &Downloader{
		WorkerCount:  workerCount,
		requestChan:  make(chan *http.Request),
		responseChan: make(chan *http.Response),
	}
}
